package cn.mrlong.base.utils

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.provider.Settings
import android.text.TextUtils

object IntentUtils {
    @JvmOverloads
    fun startActivity(context: Context, gotoClass: Class<*>?, bundle: Bundle? = null) {
        startActivity(context, gotoClass, bundle, "", false, true)
    }

    fun startActivity(context: Context, gotoClass: Class<*>?, bundle: Bundle?, requestCode: Int) {
        startActivity(context, gotoClass, bundle, requestCode.toString(), false, true)
    }

    fun startActivity(
        context: Context,
        gotoClass: Class<*>?,
        bundle: Bundle?,
        requestCode: String,
        finishSelf: Boolean,
        withAnim: Boolean
    ) {
        val intent = Intent()
        intent.setClass(context, gotoClass!!)
        if (bundle != null) intent.putExtras(bundle)
        if (!TextUtils.isEmpty(requestCode)) (context as Activity).startActivityForResult(
            intent,
            requestCode.toInt()
        ) else context.startActivity(intent)
        if (finishSelf) (context as Activity).finish()

        //if(withAnim) ((Activity) context).overridePendingTransition(R.anim.enter_exit, R.anim.enter_enter);
    }

    fun startActivityForResult(context: Context, gotoClass: Class<*>?, requestCode: Int) {
        val intent = Intent()
        intent.setClass(context, gotoClass!!)
        (context as Activity).startActivityForResult(intent, requestCode)
        // ((Activity) context).overridePendingTransition(R.anim.enter_exit, R.anim.enter_enter);
    }

    fun finish(activity: Activity) {
        activity.finish()
        // activity.overridePendingTransition(R.anim.exit_enter, R.anim.exit_exit);
    }

    /**
     * 跳转到浏览器
     *
     * @param url
     */
    fun startActivityBrowser(context: Context, url: String?) {
        val intent = Intent()
        intent.action = "android.intent.action.VIEW"
        val content_url = Uri.parse(url)
        intent.data = content_url
        val pm = context.packageManager
        val list = pm.queryIntentActivities(intent, 0)
        if (list.size > 0) {
            context.startActivity(intent)
        }
    }

    /**
     * 跳转到设置页面
     *
     * @param activity
     */
    fun startActivitySettings(activity: Activity) {
        try {
            val intent = Intent(Settings.ACTION_SETTINGS)
            activity.startActivity(intent)
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    /**
     * 跳转到系统邮箱
     *
     * @param context
     */
    fun startActivityEmail(context: Context, uriString: String?, title: String?) {
        val uri = Uri.parse(uriString) //mailto:support@qq.com
        val intent = Intent(Intent.ACTION_SENDTO, uri)
        intent.putExtra(Intent.EXTRA_SUBJECT, "subject")
        intent.putExtra(Intent.EXTRA_TEXT, "content")
        context.startActivity(Intent.createChooser(intent, title))
    }
}
